import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:wanchong/app/app.dart';
import 'package:wanchong/conf.dart';
import 'package:wanchong/utils/http.dart';

alert(String msg) {
  Fluttertoast.showToast(
      msg: msg, toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.CENTER);
}
getToken() async {
  SharedPreferences _prefs = await SharedPreferences.getInstance();
  return _prefs.getString("token");
}
setToken(String token) async {
  SharedPreferences _prefs = await SharedPreferences.getInstance();
  _prefs.setString("token", token);
}

removeToken() async {
  ///删除http的请求头
  Http.Token = "";

  ///删除缓存的请求头
  SharedPreferences _prefs = await SharedPreferences.getInstance();
  _prefs.remove("token");
}

getIm() async {
  SharedPreferences _prefs = await SharedPreferences.getInstance();
  var im = _prefs.getString("im");
  var sig = _prefs.getString("sig");
  return ImUser(nick: im,sig: sig);
}
removeIm() async {
  SharedPreferences _prefs = await SharedPreferences.getInstance();
  _prefs.remove("im");
  _prefs.remove("sig");
}

setIm(String im, String sig) async {
  SharedPreferences _prefs = await SharedPreferences.getInstance();
  _prefs.setString("im", im);
  _prefs.setString("sig", sig);
}
initHistory() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  history = prefs.getStringList(HistoryKey);
}

addHistory(String keys) async {
  history.add(keys);
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setStringList(HistoryKey, history);
}

launchPhone({String phone}) async {
  String url = "tel:$phone";
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    alert("没有通话权限,请手动拨打");
  }
}

///格式化文件大小
renderSize(double value) {
  if (null == value) {
    return 0;
  }
  List<String> unitArr = List()..add('B')..add('K')..add('M')..add('G');
  int index = 0;
  while (value > 1024) {
    index++;
    value = value / 1024;
  }
  String size = value.toStringAsFixed(2);
  return size + unitArr[index];
}
///第一个测试账号
  Future<void> dimLogin(String nick,String sig) async {
    var result = await dim.imLogin(nick, sig);
    print(result);
  }


